<?php
/**
 * 标签管理 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_tag.php 523 2010-06-23 10:06:24Z lisijie86 $
*/

!defined('IN_ADMIN') && exit('Access Denied!');

check_perm_prompt('tag','manage');

empty($ac) && $ac = 'list';

//标签列表
if ( $ac == 'list' ) {
	initGP(array('page','keyword','min','max'));
	$page = max(1, intval($page));
	$pagesize = 20;
	$offset = ($page-1) * $pagesize;
	$url = "?mod=tag&keyword=".rawurlencode($keyword)."&min={$min}&max={$max}";
	$wheresql = '1';
	if ( $keyword ) $wheresql .= " AND tagname LIKE '%$keyword%'";
	if ( $min && is_numeric($min) ) $wheresql .= " AND `count` >= '$min'";
	if ( $max && is_numeric($max) ) $wheresql .= " AND `count` <= '$max'";
	$count = $db->count('tag', $wheresql);
	$result = array();
	if ( $count ) {
		$result = $db->fetch_all("SELECT * FROM ".tname('tag')." WHERE $wheresql ORDER BY id DESC LIMIT $offset, $pagesize");
	}
	$tab['list'] = ' class="on"';
//添加&&编辑
} elseif ( $ac == 'add' || $ac == 'edit' ) {
	if ( check_submit() ) {
		initGP(array('id','tagname','P'));
		$id = intval($id);
		$tagname = check_str($tagname);
		if ( empty($tagname) ) prompt(__('标签名称不能为空。'));
		if ( $db->result("SELECT id FROM ".tname('tag')." WHERE tagname = '$tagname' AND id != '$id'") ) {
			prompt(__('标签 <b>%s</b> 已存在。',$tagname));
		}
		if ( $id ) {
			$db->update('tag', array('tagname'=>$tagname), array('id'=>$id));
		} else {
			$db->insert('tag', array('tagname'=>$tagname));
		}
		redirect('?mod=tag');
	} else {
		$result = array();
		if ( $_GET['id'] && is_numeric($_GET['id']) ) {
			$result = $db->fetch_one_array("SELECT * FROM ".tname('tag')." WHERE id = '$_GET[id]'");
		}
	}
	$tab['add'] = ' class="on"';
//重新统计
} elseif ( $ac == 'recount' ) {
	$ids = getGP('id','P','array');
	if ( empty($ids) ) $ids = array(intval($_GET['id']));
	if ( $ids ) {
		foreach ( $ids as $id ) {
			$count = $db->count('tagpost', "tagid = ".intval($id));
			$db->update('tag', array('count'=>$count), array('id'=>intval($id)));
		}
	}
	redirect($_USER['refer']);
//删除标签
} elseif ( $ac == 'del' ) {
	
	$ids = getGP('id','P','array');
	if ( empty($ids) ) $ids = array(intval($_GET['id']));
	if ( $ids ) {
		foreach ( $ids as $id ) {
			$id = (int)$id;
			/*
			$tagname = $db->result("SELECT tagname FROM ".tname('tag')." WHERE id = '$id'");
			//读取使用该标签的帖子id
			$query = $db->query("SELECT postid FROM ".tname('tagpost')." WHERE tagid = '$id'");
			while ( $row = $db->fetch_array($query) ) {
				$postids[] = $row['postid'];
			}
			if ( $postids ) {
				$db->query("UPDATE ".tname('post')." SET tags = '' WHERE id IN (".simplode($postids).") AND tags = '$tagname'");
				$db->query("UPDATE ".tname('post')." SET tags = REPLACE(tags, ',$tagname', '') WHERE id IN (".simplode($postids).")");
			}*/
			$db->query("DELETE FROM ".tname('tag')." WHERE id = '$id'");
			$db->query("DELETE FROM ".tname('tagpost')." WHERE tagid = '$id'");
		}
	}
	redirect($_USER['refer']);
	
//合并
} elseif ( $ac == 'merge' ) {
	$newtag = check_str($_POST['newtag']);
	if ( strlen($newtag) < 1 || strlen($newtag) > 20 ) {
		prompt(__('标签长度应在1-20字节之间。'));
	}
	$tagid = $db->result("SELECT id FROM ".tname('tag')." WHERE tagname = '$newtag'");
	if ( !$tagid ) {
		$tagid = $db->insert('tag', array('tagname'=>$newtag));
	}
	$ids = array();
	foreach ( $_POST['id'] as $key => $val ) {
		if ( $val && is_numeric($val) && $val != $tagid ) {
			$ids[] = $val;
		}
	}
	$db->update('tagpost', array('tagid'=>$tagid), array('tagid'=>$ids));
	$db->delete('tag', array('id'=>$ids));
	redirect('?mod=tag');
}
include admin_tpl('tag');
?>